//
//  HMStatusPictureView.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//cell可重用标示
private let HMStatusPictureViewCellId = "HMStatusPictureViewCellId"
  //cell间距宽高
private let margin: CGFloat = 5
private let cellWH: CGFloat = 80
class HMStatusPictureView: UICollectionView {
    //外界赋值
    var picInfos:[HMPictureInfoModel]?{
        didSet{
        //赋值
            contentLab.text = "\(picInfos?.count ?? 0)"
        //得到size
            let size = getPictureViewSize(picInfos?.count ?? 0)
            //更新约束
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            //刷新
            reloadData()
            
        }
    }
    //计算配图
    func getPictureViewSize(count: Int) -> CGSize{
    //行数列数
      
        //三目运算符
        let col = count == 4 ? 2 : count > 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1 )/3 + 1
        
        //计算宽高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * margin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * margin
        
        return CGSize(width: w, height:h)
    
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
       backgroundColor = UIColor.clearColor()
       addSubview(contentLab)
    
        
        
        contentLab.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        //注册标示
        registerClass(HMStatusPictureViewCell.self, forCellWithReuseIdentifier: "HMStatusPictureViewCellId")
        //数据源方法
        dataSource = self
        //设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置itemsSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        //设置水平垂直距离
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        
        
    }
    
    //懒加载控件
    private lazy var contentLab: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35)
    
}

//傻逼写错了 记住
extension HMStatusPictureView:  UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
       return picInfos?.count ?? 0
    }

   
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("HMStatusPictureViewCellId", forIndexPath: indexPath) as! HMStatusPictureViewCell
    
        //cell赋值
        cell.picUrl = picInfos![indexPath.item ]
        
        return cell
    }
 
}
//自定义Cell
class HMStatusPictureViewCell: UICollectionViewCell {
    //外界访问
    var picUrl : HMPictureInfoModel?{
        didSet{
          bgImageView.hm_setImageWithString(picUrl?.thumbnail_pic, placeholderImageName: "weibo_placeholder")
        //判断是否是GIF图片
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif"){
            //cell复用问题
            gifImageView.hidden = false
            }else{
            //cell复用问题
            gifImageView.hidden = true
            
            }
            
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 // MARK: - 设置视图
    private func setupUI(){
    
     backgroundColor = UIColor.whiteColor()
        //添加控件
       contentView.addSubview(bgImageView)
       contentView.addSubview(gifImageView)
        
        //添加约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) in
            make.bottom.trailing.equalTo(contentView)
        }
    
    }
    //懒加载图片

    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(imageName:"weibo_placeholder")
         img.contentMode = .ScaleToFill
         img.clipsToBounds = true
        return img
    }()
    //gitt图片
     private lazy var gifImageView: UIImageView = UIImageView(imageName: "timeline_image_gif")

}



